<?php
namespace Home\Controller;
use Think\Controller;
header("Content-type:text/html;charset=utf-8");
class NotifyController extends Controller {
    public function zhitongbao_notify(){
        include_once("public/payapi/zhitongbao/merchant.php");
        $merchant_code	= $_POST["merchant_code"];
        $notify_type = $_POST["notify_type"];
        $notify_id = $_POST["notify_id"];
        $interface_version = $_POST["interface_version"];
        $sign_type = $_POST["sign_type"];
        $dinpaySign = base64_decode($_POST["sign"]);
        $order_no = $_POST["order_no"];
        $order_time = $_POST["order_time"];
        $order_amount = $_POST["order_amount"];
        $extra_return_param = $_POST["extra_return_param"];
        $trade_no = $_POST["trade_no"];
        $trade_time = $_POST["trade_time"];
        $trade_status = $_POST["trade_status"];
        $bank_seq_no = $_POST["bank_seq_no"];
        /////////////////////////////   参数组装  /////////////////////////////////
        /**
        除了sign_type dinpaySign参数，其他非空参数都要参与组装，组装顺序是按照a~z的顺序，下划线"_"优先于字母
         */
        $signStr = "";
        if($bank_seq_no != ""){
            $signStr = $signStr."bank_seq_no=".$bank_seq_no."&";
        }
        if($extra_return_param != ""){
            $signStr = $signStr."extra_return_param=".$extra_return_param."&";
        }
        $signStr = $signStr."interface_version=".$interface_version."&";
        $signStr = $signStr."merchant_code=".$merchant_code."&";
        $signStr = $signStr."notify_id=".$notify_id."&";
        $signStr = $signStr."notify_type=".$notify_type."&";
        $signStr = $signStr."order_amount=".$order_amount."&";
        $signStr = $signStr."order_no=".$order_no."&";
        $signStr = $signStr."order_time=".$order_time."&";
        $signStr = $signStr."trade_no=".$trade_no."&";
        $signStr = $signStr."trade_status=".$trade_status."&";
        $signStr = $signStr."trade_time=".$trade_time;
        /////////////////////////////   RSA-S验签  /////////////////////////////////
        $dinpay_public_key = openssl_get_publickey($dinpay_public_key);
        $flag = openssl_verify($signStr,$dinpaySign,$dinpay_public_key,OPENSSL_ALGO_MD5);
        //////////////////////   异步通知必须响应“SUCCESS” /////////////////////////
        /**
        如果验签返回ture就响应SUCCESS,并处理业务逻辑，如果返回false，则终止业务逻辑。
         */
        if($flag){
            $is_return  = M("order")->where("number='{$order_no}'")->find();
            if(!$is_return){
                echo "Signature error";die;
            }
            M("order")->where("number='{$order_no}'")->save(array('logs'=>json_encode($_POST)));
            if($trade_status=="SUCCESS"){
                    if($is_return['state']!=0){
                        echo "SUCCESS";die;
                    }
                    $id = $is_return['id'];
                    $dd['state'] = 1;
                    M('order')->where("id = '{$id}'")->save($dd);
                    $order = M('order')->where("id = '{$id}'")->find();
                    $Accountsinfo = M('Accountsinfo')->where("userid = ".$order['uid'])->field("userid,money")->find();
                    //往用户表里添加 money
                    M('Accountsinfo')->where("userid = ".$order['uid'])->setInc("money",$order['money']);
                    M('Accountsinfo')->where("userid = ".$order['uid'])->setInc("score",$order['money']);
                    M('Accountsinfo')->where("userid = ".$order['uid'])->setInc("sheng_money",$order['money']);
                    //往交易表里添加 数据
                    $cp['number'] = $order['number'];
                    $cp['type'] = '1';
                    $cp['shou_money'] = $order['money'];
                    $cp['ky_money'] = $Accountsinfo['money'] + $order['money'];
                    $cp['ctime'] = time();
                    $cp['uid'] = $order['uid'];
                    $cp['remark'] = '充值入款';
                    M("jiaoyi")->add($cp);
                    //往活动表里 变化
                    $co = M('Accountsinfo')->where("userid = ".$order['uid'])->field("score")->find();
                    if($co['score']>=0 && $co['score']<10){
                        $vip = 1;
                    }if($co['score']>=10 && $co['score']<200){
                        $vip = 2;
                    }if($co['score']>=200 && $co['score']<1000){
                        $vip = 3;
                    }if($co['score']>=1000 && $co['score']<10000){
                        $vip = 4;
                    }if($co['score']>=10000 && $co['score']<50000){
                        $vip = 5;
                    }if($co['score']>=50000 && $co['score']<250000){
                        $vip = 6;
                    }if($co['score']>=250000 && $co['score']<1000000){
                        $vip = 7;
                    }if($co['score']>=1000000 && $co['score']<5000000){
                        $vip = 8;
                    }if($co['score']>=5000000){
                        $vip = 9;
                    }
                    $ro = M('up_level')->where("uid = ".$order['uid'])->field("level")->find();
                    if($vip >= 3 && $order['money']>=100){
                        M('up_level')->where("uid = '{$order['uid']}' and type = 5")->save(array("kl_money"=>0,"is_lingqu"=>0,"lq_time"=>'0'));
                    }
                    if($vip!=$ro['level']){
                        M('up_level')->where("uid = ".$order['uid'])->save(array("level"=>$vip));
                        M('up_level')->where("uid = '{$order['uid']}' and type = 1")->save(array("kl_money"=>0,"is_lingqu"=>0,"lq_time"=>'0'));
                    }
                    $acc =  M('Accountsinfo')->where("userid = ".$Accountsinfo['userid'])->field("yq_code")->find();
                    $agent = M("agent")->where("code=".$acc['yq_code'])->field("uid")->select();//查邀请码
                    foreach($agent as $v){
                        $uid = $v['uid'];
                    }
                    $ag = M("agent")->where("uid={$uid}")->field("code")->select();//查邀请码
                    foreach($ag as $v){
                        $code[] = $v["code"];
                    }
                    $codes = implode(",",$code);
                    //查找所有code下注册的账号
                    if(!empty($codes)){
                        $Accountsinfo1 = M('Accountsinfo')->where("yq_code in($codes)")->field("score")->select();
                    }
                    $zong = 0;
                    foreach($Accountsinfo1 as $v){
                        $zong += $v['score'];
                    }
                    $lingqu = M('up_level')->where("uid = '{$uid}' and type = 7")->find();
                    if($lingqu['kl_money']==58.00){}elseif($zong>=5000){
                        M('up_level')->where("uid = '{$uid}' and type = 7")->save(array("kl_money"=>0,"is_lingqu"=>0,"lq_time"=>'0'));
                    }
            }
            echo "SUCCESS";
        }else{
            echo "Signature error";
        }
    }
    public function duodebao_notify(){
        include_once("public/payapi/duodebao/merchant.php");
        $merchant_code	= $_POST["merchant_code"];
        $notify_type = $_POST["notify_type"];
        $notify_id = $_POST["notify_id"];
        $interface_version = $_POST["interface_version"];
        $sign_type = $_POST["sign_type"];
        $dinpaySign = base64_decode($_POST["sign"]);
        $order_no = $_POST["order_no"];
        $order_time = $_POST["order_time"];
        $order_amount = $_POST["order_amount"];
        $extra_return_param = $_POST["extra_return_param"];
        $trade_no = $_POST["trade_no"];
        $trade_time = $_POST["trade_time"];
        $trade_status = $_POST["trade_status"];
        $bank_seq_no = $_POST["bank_seq_no"];
        /////////////////////////////   参数组装  /////////////////////////////////
        /**
        除了sign_type dinpaySign参数，其他非空参数都要参与组装，组装顺序是按照a~z的顺序，下划线"_"优先于字母
         */
        $signStr = "";
        if($bank_seq_no != ""){
            $signStr = $signStr."bank_seq_no=".$bank_seq_no."&";
        }
        if($extra_return_param != ""){
            $signStr = $signStr."extra_return_param=".$extra_return_param."&";
        }
        $signStr = $signStr."interface_version=".$interface_version."&";
        $signStr = $signStr."merchant_code=".$merchant_code."&";
        $signStr = $signStr."notify_id=".$notify_id."&";
        $signStr = $signStr."notify_type=".$notify_type."&";
        $signStr = $signStr."order_amount=".$order_amount."&";
        $signStr = $signStr."order_no=".$order_no."&";
        $signStr = $signStr."order_time=".$order_time."&";
        $signStr = $signStr."trade_no=".$trade_no."&";
        $signStr = $signStr."trade_status=".$trade_status."&";
        $signStr = $signStr."trade_time=".$trade_time;
        /////////////////////////////   RSA-S验签  /////////////////////////////////
        $dinpay_public_key = openssl_get_publickey($dinpay_public_key);
        $flag = openssl_verify($signStr,$dinpaySign,$dinpay_public_key,OPENSSL_ALGO_MD5);
        //////////////////////   异步通知必须响应“SUCCESS” /////////////////////////
        /**
        如果验签返回ture就响应SUCCESS,并处理业务逻辑，如果返回false，则终止业务逻辑。
         */
        if($flag){
            $is_return  = M("order")->where("number='{$order_no}'")->find();
            if(!$is_return){
                echo "Signature error";die;
            }
            M("order")->where("number='{$order_no}'")->save(array('logs'=>json_encode($_POST)));
            if($trade_status=="SUCCESS"){
                if($is_return['state']!=0){
                    echo "SUCCESS";die;
                }
                $id = $is_return['id'];
                $dd['state'] = 1;
                M('order')->where("id = '{$id}'")->save($dd);
                $order = M('order')->where("id = '{$id}'")->find();
                $Accountsinfo = M('Accountsinfo')->where("userid = ".$order['uid'])->field("userid,money")->find();
                //往用户表里添加 money
                M('Accountsinfo')->where("userid = ".$order['uid'])->setInc("money",$order['money']);
                M('Accountsinfo')->where("userid = ".$order['uid'])->setInc("score",$order['money']);
                M('Accountsinfo')->where("userid = ".$order['uid'])->setInc("sheng_money",$order['money']);
                //往交易表里添加 数据
                $cp['number'] = $order['number'];
                $cp['type'] = '1';
                $cp['shou_money'] = $order['money'];
                $cp['ky_money'] = $Accountsinfo['money'] + $order['money'];
                $cp['ctime'] = time();
                $cp['uid'] = $order['uid'];
                $cp['remark'] = '充值入款';
                M("jiaoyi")->add($cp);
                //往活动表里 变化
                $co = M('Accountsinfo')->where("userid = ".$order['uid'])->field("score")->find();
                if($co['score']>=0 && $co['score']<10){
                    $vip = 1;
                }if($co['score']>=10 && $co['score']<200){
                    $vip = 2;
                }if($co['score']>=200 && $co['score']<1000){
                    $vip = 3;
                }if($co['score']>=1000 && $co['score']<10000){
                    $vip = 4;
                }if($co['score']>=10000 && $co['score']<50000){
                    $vip = 5;
                }if($co['score']>=50000 && $co['score']<250000){
                    $vip = 6;
                }if($co['score']>=250000 && $co['score']<1000000){
                    $vip = 7;
                }if($co['score']>=1000000 && $co['score']<5000000){
                    $vip = 8;
                }if($co['score']>=5000000){
                    $vip = 9;
                }
                $ro = M('up_level')->where("uid = ".$order['uid'])->field("level")->find();
                if($vip >= 3 && $order['money']>=100){
                    M('up_level')->where("uid = '{$order['uid']}' and type = 5")->save(array("kl_money"=>0,"is_lingqu"=>0,"lq_time"=>'0'));
                }
                if($vip!=$ro['level']){
                    M('up_level')->where("uid = ".$order['uid'])->save(array("level"=>$vip));
                    M('up_level')->where("uid = '{$order['uid']}' and type = 1")->save(array("kl_money"=>0,"is_lingqu"=>0,"lq_time"=>'0'));
                }
                $acc =  M('Accountsinfo')->where("userid = ".$Accountsinfo['userid'])->field("yq_code")->find();
                $agent = M("agent")->where("code=".$acc['yq_code'])->field("uid")->select();//查邀请码
                foreach($agent as $v){
                    $uid = $v['uid'];
                }
                $ag = M("agent")->where("uid={$uid}")->field("code")->select();//查邀请码
                foreach($ag as $v){
                    $code[] = $v["code"];
                }
                $codes = implode(",",$code);
                //查找所有code下注册的账号
                if(!empty($codes)){
                    $Accountsinfo1 = M('Accountsinfo')->where("yq_code in($codes)")->field("score")->select();
                }
                $zong = 0;
                foreach($Accountsinfo1 as $v){
                    $zong += $v['score'];
                }
                $lingqu = M('up_level')->where("uid = '{$uid}' and type = 7")->find();
                if($lingqu['kl_money']==58.00){}elseif($zong>=5000){
                    M('up_level')->where("uid = '{$uid}' and type = 7")->save(array("kl_money"=>0,"is_lingqu"=>0,"lq_time"=>'0'));
                }
            }
            echo "SUCCESS";
        }else{
            echo "Signature error";
        }
    }
}